package org.myself.meeting.controller;

import org.myself.meeting.Utils.Constant;
import org.myself.meeting.model.*;
import org.myself.meeting.service.DepartmentService;
import org.myself.meeting.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;


@Controller
public class LoginController {
    private static List<String> sessionIds = new ArrayList<String>();

    @Autowired
    EmployeeService es;
    @Autowired
    EmployeeService employeeService;

    @Autowired
    DepartmentService ds;


//    @RequestMapping("/")
//    public String login(){
//        return "login";
//    }

    @PostMapping("/dologin")
    @ResponseBody
    public Result dologin(@RequestBody NamePass namePass){
//        String md5Password=DigestUtils.md5DigestAsHex(password.getBytes());
        //c4ca4238a0b923820dcc509a6f75849b   1
        Employee employee = es.doLogin(namePass.getUsername(),namePass.getPassword());
        if(employee == null){
            return new Result(400,"用户名或者密码错误",null);
        }else{
            if (employee.getStatus() == Constant.PUPASSSTATUS){  //待审批状态的会议
                return new Result(400,"用户待审批",null);
            }else if (employee.getStatus() == Constant.PNPASSSTATUS){ //审批没有通过
                return new Result(400,"当前用户未通过审批,请联系管理员155-3333-2222",null);
            }else{

//                if(request.getSession().getAttribute("currentuser")!=null){
//                    System.out.println("sfdg");
//                    System.out.println(httpsession.getAttribute("currentuser"));
//                    httpsession.invalidate();
//                    request.getSession().invalidate();
//                }
//
//                String id = request.getSession().getId();
//                Enumeration ids = httpsession.getSessionContext().getIds();
//                System.out.println(ids.hasMoreElements());
//                while(ids.hasMoreElements()){
//                    String value = (String)ids.nextElement();//调用nextElement方法获得元素
//                    System.out.println(value == id);
//                }

                // HttpSession session = request.getSession().getSessionContext().getSession(id);
                if(sessionIds.contains(employee.getUsername())){
                    sessionIds.remove(employee.getUsername());
                }
                sessionIds.add(employee.getUsername());
                return new Result(200,"登录成功",employee);
            }
        }
    }

//    注册功能
    @RequestMapping("/register")
    @ResponseBody
    public Result register(){
        //提取出所有的部门消息给前端
        List<Department> deps = ds.getAllDeps();
        return  new Result(200,"所有部门查询成功",deps);
    }

    //注册界面
    @RequestMapping("/doreg")
    @ResponseBody
    public Result doReg(@RequestBody Employee employee){
        Integer value  = es.doReg(employee);
        if (value == 1){
            return new Result(200,"注册成功,请等待管理员审核",null);
        }else{
            return new Result(400,"注册失败,请检查您的信息~",employee);
        }

    }

    //退出登录
    @PostMapping("/outlogin")
    @ResponseBody
    public Result outLogin(@RequestBody Base base){
        //退出
        sessionIds.remove(base.getUsername());
        return new Result(200,"退出成功",null);
    }


//    //修改密码
//    @RequestMapping("/changepassword")
//    public String changePassword(Model model,HttpSession session){
//        return "changepassword";
//    }


    //修改密码更新数据库
    @RequestMapping("/changepwup")
    @ResponseBody
    public Result changPwUp(@RequestBody ChangPwUpAcc acc ){
        Employee emp =employeeService.getEmpsByUsername(acc.getUsername()) ;
        Integer value = es.changPwUp(emp,acc.getPassword(),acc.getNewpassword());
        if(value == 1){
            sessionIds.remove(acc.getUsername());
            return  new Result(200,"success请重新登录！",null);
        }
        return  new Result(200,"faild密码更改失败,您的输入有误！",null);
    }

    //升职功能
    @RequestMapping("/personal/promoted")
    @ResponseBody
    public Result promoted(@RequestBody Base base){
        Employee emp =employeeService.getEmpsByUsername(base.getUsername()) ;
        //更新升职状态
        Employee ep = es.getEmpById(emp.getEmployeeid());
        Map<String,Employee> map = new HashMap<String, Employee>();
        map.put("emp",ep);
        return new Result(200,"success",map);
    }

    //升职功能的处理
    @RequestMapping("/dealpromoted")
    @ResponseBody
    public Result dealPromoted(@RequestBody Base base){
        Employee emp =employeeService.getEmpsByUsername(base.getUsername()) ;
        if(emp.getRole() >= 2){
            return new Result(400,"status当前用户不允许升级",null);
        }
        Integer value = es.submitPromoted(emp,Constant.PUPSTATUS,1);
        String str = (value==1)?"申请成功,请尽快通知上一级批准":"申请失败";
        return new Result(200,"status"+str,null);
    }

    //取消升职
    @RequestMapping("/personal/cancel/promoted")
    @ResponseBody
    public Result cancelPromoted(@RequestBody Base base){
        Employee emp =employeeService.getEmpsByUsername(base.getUsername()) ;
        Integer value = es.submitPromoted(emp,Constant.PPASSSTATUS,2);
        String str = (value==1)?"操作成功":"操作失败";
        return new Result(200,str,null);
    }
}
